package com.gdth.bank.ui;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import android.app.ProgressDialog;
import android.content.Context;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

import com.gdth.bank.R;
import com.gdth.bank.activity.CjkhjlActivity;
import com.gdth.bank.adapter.JtcyXxbListAdapter;
import com.gdth.bank.adapter.JtcyXxbListAdapter.OnJtcyXxDetailClickListener;
import com.gdth.bank.event.CjkhjlListEvent;
import com.gdth.bank.presenter.TAppBfrjJtcyCpxqPresenter;
import com.gdth.bank.presenter.TAppBfrjJtcybPresenter;
import com.gdth.bank.utils.AppUtil;
import com.gdth.bank.view.TAppBfrjJtcyCpxqView;
import com.gdth.bank.view.TAppBfrjJtcybView;
import com.gdth.bank.widget.BankDrawDownListTextView;
import com.gdth.bank.widget.BankEditText;
import com.gdth.bank.widget.JtcyZcDialog;
import com.gdth.core.annotation.view.ViewInject;
import com.gdth.core.widget.BaseFrameLayout;
import com.gdth.database.bean.TAppBfrjCpxq;
import com.gdth.database.bean.TAppBfrjJtcyb;
import com.gdth.database.bean.TAppBfrjZcxxb;
import com.gdth.model.ImageItem;
import com.gdth.model.Item;
import com.gdth.utils.RegularUtil;
import com.gdth.utils.UUIDUtil;
import com.gdth.widget.dialog.BaseToast;
import com.gdth.widget.dialog.DialogConfig;

import org.greenrobot.eventbus.EventBus;

public class CjkhJtcyFragment extends BaseFrameLayout implements TAppBfrjJtcybView,TAppBfrjJtcyCpxqView{
	@ViewInject(id=R.id.cjkh_jtcy_xm) BankEditText txt_xm;
	@ViewInject(id=R.id.cjkh_jtcy_zjlb) BankDrawDownListTextView txt_zjlb;
	@ViewInject(id=R.id.cjkh_jtcy_zjhm) BankEditText txt_zjhm;
	@ViewInject(id=R.id.cjkh_jtcy_nsr) BankEditText cjkh_jtcy_nsr;
	@ViewInject(id=R.id.cjkh_jtcy_gx) BankDrawDownListTextView drawDownList_gx;
	@ViewInject(id=R.id.cjkh_jtcy_zy) BankDrawDownListTextView cjkh_jtcy_zy;
	@ViewInject(id=R.id.cjkh_jtxx_listview, itemClick="onItemClick") ListView jtxx_listview;
	@ViewInject(id=R.id.jtcy_save)
	Button jtcy_save;
	@ViewInject(id=R.id.jtcy_add)
	Button jtcy_add;
	@ViewInject(id=R.id.jtcy_change)
	Button jtcy_change;
	@ViewInject(id = R.id.cjkh_jtcy_bz)
	EditText cjkh_jtcy_bz;

	List<Item> zjList, jtgxList,cyzylist;

	List<ImageItem> items;
	TAppBfrjJtcybPresenter  mTAppBfrjJtcybPresenter;
	TAppBfrjJtcyCpxqPresenter    mTAppBfrjJtcyCpxqPresenter;
	private String jcuuid;
	JtcyXxbListAdapter adapter;
	/** 提示框 */
    ProgressDialog mProgressDialog;
    JtcyZcDialog config;
    String jtcyUUid,isCheck;
    RegularUtil checkCard;
	String hquuid="";
	private CjkhjlActivity activity;
	
	public CjkhJtcyFragment(Context context){
		super(context, R.layout.fragment_cjkhjl_jtcy);
		this.activity = (CjkhjlActivity) context;
		initializeView();
		init();
	}
	
	public void initializeView(){
		zjList= new ArrayList<Item>();
//		Item item=new Item();
//		item.setKey("1");
//		item.setValue("身份证");
//		zjList.add(item);
//		txt_zjlb.setItem(zjList);
//		txt_zjlb.setValue("1");
		jtgxList = AppUtil.getCanshuByType(context, AppUtil.SHARE_DATA_JTGX);
		cyzylist = AppUtil.getCanshuByType(context,AppUtil.SHARE_DATA_CYZY);
		cjkh_jtcy_zy.setLabel("职 业：");
		cjkh_jtcy_zy.setItem(cyzylist);
		cjkh_jtcy_zy.setValue("1");
		mTAppBfrjJtcybPresenter = new TAppBfrjJtcybPresenter(this);
		mTAppBfrjJtcyCpxqPresenter= new TAppBfrjJtcyCpxqPresenter(this);
        mProgressDialog = new  ProgressDialog(this.getContext());
        checkCard = new RegularUtil();
		jtcy_save.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View view) {
				save();
			}
		});
		jtcy_add.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				reset();
			}
		});
		jtcy_change.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				edit();
			}
		});
	}
	public void init(){
		txt_xm.setLabel(R.string.cjkh_jtcy_khxm);
		txt_xm.setHint(R.string.cjkh_jtcy_khxm_hint);
		txt_xm.setImage(null);
		
		//txt_zjlb.setLabel(R.string.cjkh_khxx_zjlb);

		txt_zjhm.setLabel(R.string.cjkh_jtcy_zjhm);
		txt_zjhm.setHint(R.string.cjkh_jtcy_zjhm_hint);
		txt_zjhm.setImage(null);
		cjkh_jtcy_nsr.setLabel("年收入：");
		cjkh_jtcy_nsr.setHint("请输入年收入（非必填）");
		cjkh_jtcy_nsr.setImage(null);

		
		drawDownList_gx.setLabel(R.string.cjkh_jtcy_gx);
		drawDownList_gx.setItem(jtgxList);
		drawDownList_gx.setValue("1");

		items = new ArrayList<ImageItem>();
		adapter = new JtcyXxbListAdapter(this.getContext(), items);
		adapter.setmOnJtcyXxDetailClickListener(new OnDetailClickListener());
		/*jtxx_listview.setAdapter(adapter);*/
		jtxx_listview.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				// TODO Auto-generated method stub
				ImageItem tImageItem = items.get(position);
				String jtuuIdKey=tImageItem.getKey();
				String jtuuIdMc = tImageItem.getImg();
				mTAppBfrjJtcybPresenter.getjtcyUuid(jtuuIdKey);
                Noedit();
			}
		});
		jtxx_listview.setAdapter(adapter);
	}
	private long lastTime = 0;
	public void save(){
		if(System.currentTimeMillis() - lastTime  < 1000) {
			return ;
		}
		lastTime = System.currentTimeMillis();

		if(txt_xm.getValue().toString().equals("") || txt_zjhm.getValue().toString().equals("") || drawDownList_gx.getValue().toString().equals("")||cjkh_jtcy_zy.getValue().toString().equals("")  ){
			Toast.makeText(this.getContext(), "请将信息填写完整", Toast.LENGTH_SHORT).show();
			return;
		}
		try {
			isCheck=checkCard.IDCardValidate(txt_zjhm.getValue().toString());
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		if(! isCheck.toString().trim().equals("")){
			Toast.makeText(this.getContext(), "请输入正确身份证", Toast.LENGTH_SHORT).show();
			return;
		}

		if(hquuid.equals("")){
			EventBus.getDefault().post(new CjkhjlListEvent(CjkhjlListEvent.refreshList));
			TAppBfrjJtcyb   mTAppBfrjJtcyb=new TAppBfrjJtcyb();
			mTAppBfrjJtcyb.setCyxm(txt_xm.getValue().toString());
			//mTAppBfrjJtcyb.setZjlb(Integer.parseInt(txt_zjlb.getkey().toString()));
			mTAppBfrjJtcyb.setZjlb(1);
			mTAppBfrjJtcyb.setJcuuid(jcuuid);
			String appuuid = UUIDUtil.getUUID();
			mTAppBfrjJtcyb.setUuid(appuuid);
			mTAppBfrjJtcyb.setZjhm(txt_zjhm.getValue().toString());
			mTAppBfrjJtcyb.setCjsj(new Date());
			mTAppBfrjJtcyb.setKhnsr(cjkh_jtcy_nsr.getValue().toString());
			mTAppBfrjJtcyb.setJtbz(cjkh_jtcy_bz.getText().toString());
			mTAppBfrjJtcyb.setSjzt(AppUtil.sjzt_create);
			mTAppBfrjJtcyb.setBbh(1);
			mTAppBfrjJtcyb.setJtgx(Integer.parseInt(drawDownList_gx.getkey().toString()));
			mTAppBfrjJtcyb.setKhzy(Integer.parseInt(cjkh_jtcy_zy.getkey().toString()));
			mTAppBfrjJtcybPresenter.savejtcyxx(mTAppBfrjJtcyb,jcuuid,0 , false);


		}else{
			changeJtcy.setZjhm(txt_zjhm.getValue().toString());
			changeJtcy.setKhnsr(cjkh_jtcy_nsr.getValue().toString());
			changeJtcy.setCyxm(txt_xm.getValue().toString());
			changeJtcy.setJtbz(cjkh_jtcy_bz.getText().toString());
			changeJtcy.setXgsj(new Date());
			changeJtcy.setSjzt(AppUtil.sjzt_update);
			changeJtcy.setJtgx(Integer.parseInt(drawDownList_gx.getkey().toString()));
			changeJtcy.setKhzy(Integer.parseInt(cjkh_jtcy_zy.getkey().toString()));

			mTAppBfrjJtcybPresenter.savejtcyxx(changeJtcy,jcuuid,1 ,isHUZHU);
		}
	}
	//  时间: 2020/5/9 10:08 AM ,作者: xjc, 作用: 修改
	public void edit() {
		cjkh_jtcy_zy.mEditText.setEnabled(true);
		drawDownList_gx.mEditText.setEnabled(true);
//		txt_zjlb.setEnabled(true);
		txt_xm.mEditText.setEnabled(true);
		cjkh_jtcy_nsr.mEditText.setEnabled(true);
		txt_zjhm.mEditText.setEnabled(true);
		cjkh_jtcy_bz.setEnabled(true);
	}
	//  时间: 2020/5/9 10:09 AM ,作者: xjc, 作用: 新增
	public void reset() {
		edit();
		hquuid = "";
		isHUZHU = false;
		changeJtcy = null;
		cjkh_jtcy_zy.setValue("1");
		drawDownList_gx.setValue("1");
		txt_xm.setValue("");
		cjkh_jtcy_nsr.setValue("");
		txt_zjhm.setValue("");
		cjkh_jtcy_bz.setText("");
	}

	@Override
	public void showProgressDialog(String msg) {
		// TODO Auto-generated method stub
		mProgressDialog.setMessage(msg);
		mProgressDialog.show();
	}

	@Override
	public void hideProgressDialog() {
		// TODO Auto-generated method stub
		mProgressDialog.hide();
	}

	@Override
	public void showMessage(String msg) {
		// TODO Auto-generated method stub
		BaseToast.getInstance().ShowSystemToast(this.getContext(), msg);
	}
	

	public void setJcuuid(String jcuuid) {
		
		this.jcuuid = jcuuid;
		mTAppBfrjJtcybPresenter.getjtcyxx(jcuuid);
		
	}

	@Override
	public void saveSuccess(TAppBfrjJtcyb mTAppBfrjJtcyb) {
		// TODO Auto-generated method stub
		mTAppBfrjJtcybPresenter.getjtcyxx(jcuuid);
		((CjkhjlActivity) context).updateJtcyForLxfs();
		activity.changejtcy();
		if(isHUZHU){
			mTAppBfrjJtcybPresenter.getbxxbZJHMM(jcuuid);
		}
		reset();
	}
	List<TAppBfrjJtcyb> jtcybList = new ArrayList<>();
	@Override
	public void getjtcyXx(List<TAppBfrjJtcyb> arg0) {
		// TODO Auto-generated method stub
		jtcybList.clear();
		jtcybList.addAll(arg0);
		dataChange(jtcybList);
	}

	public void dataChange(List<TAppBfrjJtcyb> List){
		items.clear();
		for(TAppBfrjJtcyb mTAppBfrjJtcyb : List){
			ImageItem mImageItem=new ImageItem();
			mImageItem.setKey(mTAppBfrjJtcyb.getUuid());
			mImageItem.setImg(mTAppBfrjJtcyb.getCyxm());
			mImageItem.setValue(mTAppBfrjJtcyb.getZjhm());
			mImageItem.setGnlj("登记");
			items.add(mImageItem);
		}
		adapter.notifyDataSetChanged();
	}

	class OnDetailClickListener implements OnJtcyXxDetailClickListener {
		@Override
		public void clickListener(int position) {
			dengji(position);
		}

		@Override
		public void clickdelListener(int position) {
			delItem(position);
		}
	}
	DialogConfig DialogConfig_delete;
	private void delItem(int position){

		DialogConfig_delete = new DialogConfig(getContext(), "是否删除该记录", true, new DeleteClickListener(position));
		DialogConfig_delete.show();
	}

	/**
	 * 删除资产信息
	 * @author arvin
	 *
	 */
	private class DeleteClickListener implements OnClickListener {
		private int position;

		public DeleteClickListener(int position) {
			this.position = position;
		}

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
//			if(jtcybList.size() > 0){
				if(activity.oldzjhm .equals(jtcybList.get(position).getZjhm())){
					BaseToast.getInstance().ShowCenterToast(getContext(),"户主不能被删除哦！");
				}else{
					mTAppBfrjJtcybPresenter.deljtcyxx(jtcybList.get(position));
				}
//			}
//			else{
//				BaseToast.getInstance().ShowCenterToast(getContext(),"家庭信息不能为空哦！");
//			}

			DialogConfig_delete.dismiss();
		}
	}

	private void dengji(int position){
		  ImageItem tImageItem = items.get(position);
		  jtcyUUid = tImageItem.getKey();
		  MyClickListener mMyClickListener= new MyClickListener();
		  config = new JtcyZcDialog(this.getContext(), "退出登录，请确认", true, mMyClickListener);
		  config.setCpZlXx(jtcyUUid);
		  config.show();
		  
		  
	   	}
	 class  MyClickListener implements OnClickListener{

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			List<TAppBfrjCpxq> mTAppBfrjCpxqData = config.getCpZlXx(jtcyUUid,jcuuid);
			mTAppBfrjJtcyCpxqPresenter.savejtcyxxCpxq(mTAppBfrjCpxqData);
			
		}
		 
	 }
	@Override
	public void saveSuccess(List<TAppBfrjCpxq> mTAppBfrjCpxqList) {
		// TODO Auto-generated method stub
		config.dismiss();

	}

	@Override
	public void getjtcyCpxqXx(List<TAppBfrjCpxq> arg0) {
		// TODO Auto-generated method stub
		
	}
	TAppBfrjJtcyb changeJtcy;
	private boolean isHUZHU = false;
	@Override
	public void getjtcyXxUuid(TAppBfrjJtcyb arg0) {
		// TODO Auto-generated method stub
		if(arg0!=null){
			txt_xm.setValue(arg0.getCyxm());
//			txt_zjlb.setValue(arg0.getZjlb().toString());
			if(activity.oldzjhm.equals(arg0.getZjhm())){
				isHUZHU = true;
			}else{
				isHUZHU = false;
			}
			txt_zjhm.setValue(arg0.getZjhm());
			drawDownList_gx.setValue(arg0.getJtgx().toString());
			if(arg0.getKhzy() == null || arg0.getKhzy().toString().equals("")){
				cjkh_jtcy_zy.setValue("1");
			}else{
				cjkh_jtcy_zy.setValue(arg0.getKhzy().toString());
			}
			if(arg0.getKhnsr() == null || arg0.getKhnsr().toString().equals("")){
				cjkh_jtcy_nsr.setValue(arg0.getKhnsr());
			}else{
				cjkh_jtcy_nsr.setValue(arg0.getKhnsr());
			}
			if(arg0.getKhnsr() == null || arg0.getKhnsr().toString().equals("")){
				cjkh_jtcy_bz.setText("");
			}else{
				cjkh_jtcy_bz.setText(arg0.getJtbz());
			}
			hquuid = arg0.getUuid();
			changeJtcy = arg0;
		}
	}

	public void Noedit() {
		cjkh_jtcy_zy.mEditText.setEnabled(false);
		drawDownList_gx.mEditText.setEnabled(false);
//		txt_zjlb.setEnabled(true);

		txt_xm.mEditText.setEnabled(false);
		cjkh_jtcy_nsr.mEditText.setEnabled(false);
		txt_zjhm.mEditText.setEnabled(false);
		cjkh_jtcy_bz.setEnabled(false);
	}

	@Override
	public void deleteSuccess(TAppBfrjJtcyb argo) {
		this.jtcybList.remove(argo);
		dataChange(this.jtcybList);
		activity.changejtcy();
        ((CjkhjlActivity) context).updateJtcyForLxfs();
		mTAppBfrjJtcybPresenter.getbxxbZJHMM(jcuuid);
	}

	@Override
	public void deletefalse(String arg0) {
//		if(arg0.equals("0")){
//			BaseToast.getInstance().ShowCenterToast(this.getContext(),"户主不能删除哦！");
//		}else{
		BaseToast.getInstance().ShowCenterToast(this.getContext(),"资产信息中以使用，不能删除哦！");
//		}

	}

	@Override
	public void getbxxbZjhm(String arg0) {
		activity.oldzjhm = arg0;
	}
}
